﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.IO;

public partial class QuarterPlan_QuarterPlanAdd : System.Web.UI.Page
{
    IBussinessLogic dllDepartment = new DepartmentInfoManager();
    UserInfoManager dllUserInfo = new UserInfoManager();
    QuarterPlanofManager dllQuarterPlanofManager = new QuarterPlanofManager();
    Authentication dllAuthentication = new Authentication();
    VerifyCircuitManager dllVerifyCircuitManager = new VerifyCircuitManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    QuarterPlanDetailManager dllQuarterPlanDetail = new QuarterPlanDetailManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        //判断是否登录
        dllAuthentication.isLogin();
        this.gdvQuarterPlan.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        this.gdvQuarterPlan.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        this.btnSave.Enabled = false;
        this.btnOk.Enabled = false;
        this.btnQuarterResolve.Enabled = false;
        _powerTable = dllAuthentication.getPower(objUserInfo.UserName, "添加年计划");
        for (int i = 0; i < _powerTable.Rows.Count; i++)
        {
            if (_powerTable.Rows[i]["PowerName"].ToString() == "新建")
            {
                this.btnSave.Enabled = true;
                this.btnOk.Enabled = true;
                this.btnQuarterResolve.Enabled = true;
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "删除")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "查看")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "修改")
            {
            }
        }


        if (!IsPostBack)
        {
            #region 弹出提醒
            PopupManager dllPopupManager = new PopupManager();
            string _info = dllPopupManager.getPopupInfo();
            if (_info.Length > 0)
            {
                ShowMessage(_info);
            }
            #endregion
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpDepartment);
            bindDropDownList(this.drpQuarterDate);
            bindDropDownList(this.drpMasterPeople);
            //默认绑定责任部门文本框
            this.txtMasterDepartment.Text = this.drpDepartment.SelectedItem.Text;
            bindGridViewQuarterPlan();

        }


    }

    /// <summary>
    /// 绑定已有年计划用于显示
    /// </summary>
    void bindGridViewQuarterPlan()
    {
        object[] _value = new object[2];
        //_value[0] = objUserInfo.UserName;
        _value[0] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
        _value[1] = this.drpDepartment.SelectedItem.Text;
        DataTable _quarterPlan = dllQuarterPlanofManager.get(QuarterPlanofManager.field.DepartmentQuarterDate.ToString(), _value);
        DataView dv = new DataView(_quarterPlan);
         dv.Sort = "QuarterPlanSortId";
        this.gdvQuarterPlan.DataSource = dv;
        this.gdvQuarterPlan.DataBind();
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            LinkButton lbtn = (LinkButton)gdvQuarterPlan.Rows[i].Cells[4].FindControl("lbtnSource");
            lbtn.Text = gdvQuarterPlan.DataKeys[i]["Source"].ToString();
            wrapByComma(this.gdvQuarterPlan, i, 7);
            wrapByComma(this.gdvQuarterPlan, i, 8);
            if (this.gdvQuarterPlan.Rows[i].Cells[11].Text == "&amp;nbsp;")
            {
                this.gdvQuarterPlan.Rows[i].Cells[11].Text = "";
            }
        }
        this.txtQuarterPlanSortId.Value =dllQuarterPlanofManager.getPlanSort(this.drpQuarterDate.SelectedValue)+"."+ Convert.ToString(this.gdvQuarterPlan.Rows.Count + 1);
        #region 下载文件
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        { 
            if (this.gdvQuarterPlan.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            {
                Button _btn = (Button)this.gdvQuarterPlan.Rows[i].Cells[12].FindControl("btnDownLoad");
                _btn.Visible = false;
                Label _lbl = (Label)this.gdvQuarterPlan.Rows[i].Cells[12].FindControl("lblAlert");
                _lbl.Visible = true;
                _lbl.Text = "无附件";
            }
             
        }
        #endregion
    }

    /// <summary>
    /// 弹出小窗体事件
    /// </summary>
    /// <param name="msg"></param>
    public void ShowMessage(string msg)
    {
        EeekSoft.Web.PopupWin pop = new EeekSoft.Web.PopupWin();
        this.Controls.Add(pop);
        pop.Text = msg;
        pop.Message = msg;
        pop.Title = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        pop.WindowScroll = true;
        pop.HideAfter = 99999999;
        pop.DockMode = EeekSoft.Web.PopupDocking.BottomLeft;
        pop.OffsetX = int.Parse(Session["_width"].ToString()) - 338;
        pop.ActionType = EeekSoft.Web.PopupAction.OpenLink;
        pop.OffsetY = 1;
        pop.Width = 220;
        pop.Height = 170;
    }


    /// <summary>
    /// 根据部门自动赋值
    /// </summary>
    void BindDepartmentInfo()
    {
        object[] _valueDepartment = new object[1];
        _valueDepartment[0] = Convert.ToInt32(this.drpDepartment.SelectedValue);
        DataTable _department = new BLL.DepartmentInfoManager().get(DepartmentInfoManager.field.DepartmentId.ToString(), _valueDepartment);
        if (_department.Rows.Count > 0)
        {
            this.txtVerifyPeople.Value = _department.Rows[0]["DefaultEvaluatePeople"].ToString();
            this.txtSlaveDepartment.Value = _department.Rows[0]["SlaveDepartment"].ToString();
        }
    }

    /// <summary>
    /// 创建空DataTable用于绑定GridView
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    DataTable createDataTable()
    {
        DataTable _dataTable = new DataTable();
        _dataTable.Columns.Add("QuarterPlanId");
        _dataTable.Columns.Add("QuarterPlanSortId");
        _dataTable.Columns.Add("StartTime");
        _dataTable.Columns.Add("CompleteTime");
        _dataTable.Columns.Add("WorkItem");
        _dataTable.Columns.Add("MasterDepartment");
        _dataTable.Columns.Add("MasterPeople");
        _dataTable.Columns.Add("SlaveDepartment");
        _dataTable.Columns.Add("SlaveDepartmentDuty");
        _dataTable.Columns.Add("EvaluatePeople");
        _dataTable.Columns.Add("Remart");
        _dataTable.Columns.Add("PlanSendState");
        _dataTable.Columns.Add("Source");
        _dataTable.Columns.Add("TotalPlanId");
        _dataTable.Columns.Add("AttachmentPath");
        return _dataTable;
    }





    /// <summary>
    /// 绑定DropDownList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpDepartment))
        {
            object[] _value = new object[1];
            Model.UserInfo objUserInfo = new Model.UserInfo();
            _value[0] = objUserInfo.UserName;
            DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _value);
            if (_userTable.Rows.Count > 0)
            {
                if (_userTable.Rows[0]["DepartmentId"].ToString().Length > 0)
                {
                    ListItem _li = new ListItem(_userTable.Rows[0]["DepartmentId"].ToString(), _userTable.Rows[0]["DId"].ToString());
                    this.drpDepartment.Items.Add(_li);
                }
                if (_userTable.Rows[0]["PartTimeDepartmentId"].ToString().Length > 0)
                {
                    ListItem _li = new ListItem(_userTable.Rows[0]["PartTimeDepartmentId"].ToString(), _userTable.Rows[0]["PDid"].ToString());
                    this.drpDepartment.Items.Add(_li);
                }
            }
            BindDepartmentInfo();
        }
        //绑定责任人
        if (_drp.Equals(this.drpMasterPeople))
        {
            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(this.drpDepartment.SelectedItem.Value);
            BLL.DepartmentInfoManager dllDepartmentInfoManager = new DepartmentInfoManager();
            DataTable dtdepart = dllDepartment.get(DepartmentInfoManager.field.DepartmentId.ToString(), _value);
            DataTable dt = dllUserInfo.get(UserInfoManager.field.DepartmentId.ToString(), _value);
            this.drpMasterPeople.DataSource = dt;
            this.drpMasterPeople.DataTextField = "RealName";
            this.drpMasterPeople.DataValueField = "UserName";
            this.drpMasterPeople.DataBind();
            if (dtdepart.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    this.drpMasterPeople.SelectedIndex = this.drpMasterPeople.Items.IndexOf(this.drpMasterPeople.Items.FindByValue(dtdepart.Rows[0]["DepartmentMaster"].ToString()));
                    this.txtMasterPeople.Value = this.drpMasterPeople.SelectedItem.Text;
                }
            }
        }
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            //根据系统日期自动绑定到当前年份
            //string _year = DateTime.Now.Year.ToString();
            //this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
            //根据系统当前日期查找到对应的年周期
            DataTable _year = dllQuarterDateManager.getQuarterDate();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));

        }
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDate.DataSource = _quartertime;
                    this.drpQuarterDate.DataTextField = "QuarterName";
                    this.drpQuarterDate.DataValueField = "Quarter_Id";
                }
                if (Request.QueryString["Id"] != null)
                {
                    this.drpQuarterDate.SelectedValue = Utility.HttpHelp.getParamsInt("Id").ToString();
                }
                #endregion

            }
            else
            {
                ListItem li = new ListItem("无年周期", "0");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(li);
            }

        }
    }


    /// <summary>
    /// 判断控件值是否为空
    /// </summary>
    /// <returns>空返回0，有值返回1</returns>
    /// <author>grokyao</author>
    /// <date>2009-2-17</date>
    int isNull()
    {
        #region 验证是否为空
        if (this.txtQuarterPlanSortId.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtCompleteTime.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtMasterPeople.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtWorkItem.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtSlaveDepartment.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtSlaveDepartmentDuty.Text.Length < 18 && this.txtSlaveDepartment.Value != "独立完成")
        {
            return 0;
        }
        if (this.txtStartTime.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtVerifyPeople.Value.Length == 0)
        {
            return 0;
        }
        return 1;
        #endregion
    }

    /// <summary>
    /// 时间验证
    /// </summary>
    bool verifyDate()
    {
        #region 必要验证判断
        //判断完成时间是否大于起始时间

        DateTime _startTime = Convert.ToDateTime(this.txtStartTime.Text.Trim());
        DateTime _endTime = Convert.ToDateTime(this.txtCompleteTime.Text.Trim());
        if (!dllAuthentication.IsEndTimeGreaterThanStartTime(_startTime, _endTime))
        {
            Utility.HttpHelp.alert("完成时间需要大于起始时间", Page);
            return false;
        }


        return true;
        #endregion
    }


    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }

    /// <summary>
    /// 增加一行事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        int _isnull = isNull();
        if (_isnull == 0)
        {
            Utility.HttpHelp.alert("请输入完整", Page);
            return;
        }
        else
        {
             
            if (!verifyDate())
            {
                return;
            }
            if (this.txtMasterPeople.Value.Contains(","))
            {
                Utility.HttpHelp.alert("责任人不可为多人！", Page);
                return;
            }
            if (this.txtVerifyPeople.Value.Contains(","))
            {
                Utility.HttpHelp.alert("评估人不可为多人！", Page);
                return;
            }
            if (this.txtSlaveDepartment.Value != "独立完成")
            {
                if (this.txtSlaveDepartmentDuty.Text.Length < 18)
                {
                    Utility.HttpHelp.alert("配合部门责任字数不得低于32个字符", Page);
                    return;
                }
            }

            DataTable _quarterPlan = createDataTable();
            //获取GridView当前内容导入DataTable
            for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
            {
                DataRow _drGridView = _quarterPlan.NewRow();
                _drGridView["QuarterPlanId"] = this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString();
                _drGridView["PlanSendState"] = this.gdvQuarterPlan.DataKeys[i]["PlanSendState"].ToString();
                _drGridView["QuarterPlanSortId"] =  this.gdvQuarterPlan.Rows[i].Cells[1].Text.ToString() ;
                _drGridView["StartTime"] = this.gdvQuarterPlan.Rows[i].Cells[2].Text.ToString();
                _drGridView["CompleteTime"] = this.gdvQuarterPlan.Rows[i].Cells[3].Text.ToString();
                _drGridView["WorkItem"] = this.gdvQuarterPlan.Rows[i].Cells[5].Text.ToString();
                _drGridView["MasterDepartment"] = this.gdvQuarterPlan.Rows[i].Cells[6].Text.ToString();
                _drGridView["MasterPeople"] = this.gdvQuarterPlan.Rows[i].Cells[7].Text.ToString();
                _drGridView["SlaveDepartment"] = this.gdvQuarterPlan.Rows[i].Cells[8].Text.ToString();
                _drGridView["SlaveDepartmentDuty"] = this.gdvQuarterPlan.Rows[i].Cells[9].Text.ToString();
                _drGridView["EvaluatePeople"] = this.gdvQuarterPlan.Rows[i].Cells[10].Text.ToString();
                _drGridView["Remart"] = this.gdvQuarterPlan.Rows[i].Cells[10].Text.ToString();
                _drGridView["Source"] = this.gdvQuarterPlan.DataKeys[i]["Source"].ToString();
                _drGridView["TotalPlanId"] = this.gdvQuarterPlan.DataKeys[i]["TotalPlanId"].ToString();

                #region 去除特殊符号如&nbsp;
                _drGridView["PlanSendState"] = _drGridView["PlanSendState"].ToString().Replace("&", "");
                _drGridView["PlanSendState"] = _drGridView["PlanSendState"].ToString().Replace("nbsp;", "");
                _drGridView["QuarterPlanId"] = _drGridView["QuarterPlanId"].ToString().Replace("&", "");
                _drGridView["QuarterPlanId"] = _drGridView["QuarterPlanId"].ToString().Replace("nbsp;", "");
                _drGridView["QuarterPlanSortId"] = _drGridView["QuarterPlanSortId"].ToString().Replace("&", "");
                _drGridView["QuarterPlanSortId"] = _drGridView["QuarterPlanSortId"].ToString().Replace("nbsp;", "");
                _drGridView["StartTime"] = _drGridView["StartTime"].ToString().Replace("&", "");
                _drGridView["StartTime"] = _drGridView["StartTime"].ToString().Replace("nbsp;", "");
                _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("&", "");
                _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("nbsp;", "");
                _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("&", "");
                _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("nbsp;", "");

                _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("&", "");
                _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("nbsp;", "");
                _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("&", "");
                _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("nbsp;", "");
                _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace(",<br>", ",");
                _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("&", "");
                _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("nbsp;", "");
                _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace(",<br>", ",");

                _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("&", "");
                _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("nbsp;", "");
                _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("&", "");
                _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("nbsp;", "");
                _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace(",<br>", ",");

                _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("&", "");
                _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("nbsp;", "");
                _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("&amp;", "");
                _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("&nbsp;", "");

                _drGridView["Source"] = _drGridView["Source"].ToString().Replace("&", "");
                _drGridView["Source"] = _drGridView["Source"].ToString().Replace("nbsp;", "");
                _drGridView["Source"] = _drGridView["Source"].ToString().Replace("&amp;", "");
                _drGridView["Source"] = _drGridView["Source"].ToString().Replace("&nbsp;", "");

                _drGridView["TotalPlanId"] = _drGridView["TotalPlanId"].ToString().Replace("&", "");
                _drGridView["TotalPlanId"] = _drGridView["TotalPlanId"].ToString().Replace("nbsp;", "");
                _drGridView["TotalPlanId"] = _drGridView["TotalPlanId"].ToString().Replace("&amp;", "");
                _drGridView["TotalPlanId"] = _drGridView["TotalPlanId"].ToString().Replace("&nbsp;", "");
                #endregion

                _quarterPlan.Rows.Add(_drGridView);


            }
            //获取当前添加的内容

            DataRow _drTable = _quarterPlan.NewRow();
            _drTable["QuarterPlanSortId"] =this.txtQuarterPlanSortId.Value.Trim() ;
            _drTable["StartTime"] = this.txtStartTime.Text.Trim();
            _drTable["CompleteTime"] = this.txtCompleteTime.Text.Trim();
            _drTable["WorkItem"] = this.txtWorkItem.Text;
            _drTable["MasterDepartment"] = this.drpDepartment.SelectedItem.Text;
            _drTable["MasterPeople"] = this.txtMasterPeople.Value.Trim();
            _drTable["SlaveDepartment"] = this.txtSlaveDepartment.Value.Trim();
            _drTable["SlaveDepartmentDuty"] = this.txtSlaveDepartmentDuty.Text.Trim();
            _drTable["EvaluatePeople"] = this.txtVerifyPeople.Value.Trim();
            _drTable["Remart"] = this.txtRemark.Text;
            _drTable["Source"] = "新增";
            _drTable["TotalPlanId"] = 0;
            _quarterPlan.Rows.Add(_drTable);
            DataView dv = new DataView(_quarterPlan);
            dv.Sort = "QuarterPlanSortId";
            this.gdvQuarterPlan.DataSource = dv;
            this.gdvQuarterPlan.DataBind();
            //隐藏首行，只保留表头

            for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
            {
                LinkButton lbtn = (LinkButton)gdvQuarterPlan.Rows[i].Cells[4].FindControl("lbtnSource");
                lbtn.Text = gdvQuarterPlan.DataKeys[i]["Source"].ToString();
                wrapByComma(this.gdvQuarterPlan, i, 7);
                wrapByComma(this.gdvQuarterPlan, i, 8);
                if (this.gdvQuarterPlan.Rows[i].Cells[11].Text == "&amp;nbsp;")
                {
                    this.gdvQuarterPlan.Rows[i].Cells[11].Text = "";
                }
            }

            #region 善后操作
            //将文本框清空

            this.txtQuarterPlanSortId.Value = dllQuarterPlanofManager.getPlanSort(this.drpQuarterDate.SelectedValue) + "." + Convert.ToString(this.gdvQuarterPlan.Rows.Count + 1);
            this.txtCompleteTime.Text = "";
            //this.txtMasterPeople.Value = "";
            this.txtRemark.Text = "";
            this.txtSlaveDepartment.Value = "";
            this.txtSlaveDepartmentDuty.Text = "";
            this.txtStartTime.Text = "";
            this.txtVerifyPeople.Value = "";
            this.txtWorkItem.Text = "";
            #endregion
        }
        addQuarterPlan(0);
        bindGridViewQuarterPlan();
    }

    /// <summary>
    /// 保存为草稿
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>d
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //添加年计划入库
        addQuarterPlan(0);
        bindGridViewQuarterPlan();

        //清除全选按钮状态
        this.ckbAllSelect.Checked = false;
        #region 善后操作
        //将文本框清空
        this.txtQuarterPlanSortId.Value = dllQuarterPlanofManager.getPlanSort(this.drpQuarterDate.SelectedValue) + "." + Convert.ToString(this.gdvQuarterPlan.Rows.Count + 1);

        #endregion
    }


    /// <summary>
    /// 年计划提交审核，但年计划必须被分解才可提交！
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        //int _isnull = isNull();
        //if (_isnull == 0)
        //{
        //    Utility.HttpHelp.alert("请输入完整", Page);
        //    return;
        //}
        SubmitQuarterPlan(1);
        bindGridViewQuarterPlan();
        #region 善后操作
        //将文本框清空
        this.txtQuarterPlanSortId.Value = dllQuarterPlanofManager.getPlanSort(this.drpQuarterDate.SelectedValue) + "." + Convert.ToString(this.gdvQuarterPlan.Rows.Count + 1);
        this.txtCompleteTime.Text = "";
        //this.txtMasterPeople.Value = "";
        this.txtRemark.Text = "";
        this.txtSlaveDepartment.Value = "";
        this.txtSlaveDepartmentDuty.Text = "";
        this.txtStartTime.Text = "";
        this.txtVerifyPeople.Value = "";
        this.txtWorkItem.Text = "";
        #endregion


        //清除全选按钮状态
        this.ckbAllSelect.Checked = false;
    }

    /// <summary>
    /// 添加年计划方法
    /// </summary>
    /// <param name="state">0草稿 1 提交</param>
    /// <author>grokyao</author>
    /// <date>2009-2-5</date>
    void addQuarterPlan(int state)
    {
        #region 参数
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        //得到参数 部门名称的DataTable
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        DataTable _VerifyCircuitId = dllVerifyCircuitManager.getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_VerifyCircuitId.Rows.Count == 0)
        {
            Utility.Error.RedirectErrorPage("所在部门无计划流程，请重试");
        }
        //获取登录用户名
        Model.UserInfo objUserInfo = new Model.UserInfo();
        int _noSave = 0;
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            CheckBox _ckb = (CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            //if (_ckb.Checked == true)
            //{
                _count++;
                if (this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString().Length == 0 || this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString() == "&nbsp;")
                {
                    object[] _value = new object[16];
                    _value[0] = Convert.ToDateTime(this.gdvQuarterPlan.Rows[i].Cells[2].Text.Trim());
                    _value[1] = Convert.ToDateTime(this.gdvQuarterPlan.Rows[i].Cells[3].Text.Trim());
                    _value[2] = this.gdvQuarterPlan.Rows[i].Cells[5].Text.Trim();
                    _value[3] = this.gdvQuarterPlan.Rows[i].Cells[6].Text.Trim();
                    _value[4] = this.gdvQuarterPlan.Rows[i].Cells[7].Text.Trim().Replace("<br>", ",");
                    _value[5] = this.gdvQuarterPlan.Rows[i].Cells[8].Text.Trim().Replace("<br>", ",");
                    _value[6] = this.gdvQuarterPlan.Rows[i].Cells[9].Text.Trim();
                    _value[7] = state;//发送状态
                    _value[8] = 0;//是否达成共识
                    _value[9] = this.gdvQuarterPlan.Rows[i].Cells[11].Text.Trim(); ;//Remart
                    _value[10] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);//QuarterDate
                    _value[11] = Convert.ToInt32(_VerifyCircuitId.Rows[0]["VerifyCircuitId"].ToString());
                    _value[12] = objUserInfo.UserName;
                    _value[13] = this.gdvQuarterPlan.Rows[i].Cells[10].Text.Trim();
                    _value[14] =  this.gdvQuarterPlan.Rows[i].Cells[1].Text.Trim() ;
                    _value[15] = "新增";
                    _valueList.Add(_value);
                }
            //}
            //if (this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString().Length == 0)
            //{
            //    _noSave++;
            //}
        }
        #endregion
        if (_noSave > 0)
        {
            Page.RegisterStartupScript("alert", "<script>return confirm('有尚未保存的计划，继续操作可能丢失，确认要继续吗？');</script>");
        }
        if (_count <= 0)
        {
            Utility.HttpHelp.alert("请选择要保存的计划", Page);
            return;
        }
        if (_valueList.Count == 0)
        {
            Utility.HttpHelp.alert("选择的计划已保存", Page);
        }
        if (_valueList.Count > 0)
        {
            int result = dllQuarterPlanofManager.addWithTransaction(_valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加年计划未成功，请重试！");
            }


            #region 善后操作
            //将文本框清空
            this.txtQuarterPlanSortId.Value = dllQuarterPlanofManager.getPlanSort(this.drpQuarterDate.SelectedValue) + "." + Convert.ToString(this.gdvQuarterPlan.Rows.Count + 1);
            this.txtCompleteTime.Text = "";
            //this.txtMasterPeople.Value = "";
            this.txtRemark.Text = "";
            this.txtSlaveDepartment.Value = "";
            this.txtSlaveDepartmentDuty.Text = "";
            this.txtStartTime.Text = "";
            this.txtVerifyPeople.Value = "";
            this.txtWorkItem.Text = "";
            #endregion
        }
    }

    /// <summary>
    /// 添加年计划方法
    /// </summary>
    /// <param name="state">0草稿 1 提交</param>
    /// <author>grokyao</author>
    /// <date>2009-2-5</date>
    void SubmitQuarterPlan(int state)
    {
        #region 参数
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        //得到参数 部门名称的DataTable
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        DataTable _VerifyCircuitId = dllVerifyCircuitManager.getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_VerifyCircuitId.Rows.Count == 0)
        {
            Utility.Error.RedirectErrorPage("所在部门无计划流程，请重试");
        }
        //获取登录用户名
        Model.UserInfo objUserInfo = new Model.UserInfo();
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            CheckBox _ckb = (CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (_ckb.Checked == true)
            {
                _count++;
                if (this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString().Length == 0 || this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString() == "&nbsp;")
                {
                    object[] _value = new object[16];
                    _value[0] = Convert.ToDateTime(this.gdvQuarterPlan.Rows[i].Cells[2].Text.Trim());
                    _value[1] = Convert.ToDateTime(this.gdvQuarterPlan.Rows[i].Cells[3].Text.Trim());
                    _value[2] = this.gdvQuarterPlan.Rows[i].Cells[5].Text.Trim();
                    _value[3] = this.gdvQuarterPlan.Rows[i].Cells[6].Text.Trim();
                    _value[4] = this.gdvQuarterPlan.Rows[i].Cells[7].Text.Trim().Replace("<br>", ",");
                    if (this.gdvQuarterPlan.Rows[i].Cells[8].Text == "&nbsp;" || this.gdvQuarterPlan.Rows[i].Cells[8].Text == "&" || this.gdvQuarterPlan.Rows[i].Cells[8].Text == "<br>" || this.gdvQuarterPlan.Rows[i].Cells[8].Text == "")
                    {
                        Utility.HttpHelp.alert("配合部门不可为空！", Page);
                        return;
                    }
                    else
                    {
                        _value[5] = this.gdvQuarterPlan.Rows[i].Cells[8].Text.Trim().Replace("<br>", ",");
                    }
                    if (this.gdvQuarterPlan.Rows[i].Cells[8].Text.Contains("独立完成"))
                    {
                        _value[6] = this.gdvQuarterPlan.Rows[i].Cells[9].Text.Trim();
                    }
                    else
                    {
                        if (this.gdvQuarterPlan.Rows[i].Cells[9].Text == "&nbsp;" || this.gdvQuarterPlan.Rows[i].Cells[9].Text == "&" || this.gdvQuarterPlan.Rows[i].Cells[9].Text == "<br>" || this.gdvQuarterPlan.Rows[i].Cells[9].Text == "")
                        {
                            Utility.HttpHelp.alert("配合部门责任不可为空！", Page);
                            return;
                        }
                        else
                        {
                            _value[6] = this.gdvQuarterPlan.Rows[i].Cells[9].Text.Trim();
                        }
                    }
                    _value[7] = state;//发送状态
                    _value[8] = 0;//是否达成共识
                    _value[9] = this.gdvQuarterPlan.Rows[i].Cells[11].Text.Trim(); //Remart
                    _value[10] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);//QuarterDate
                    _value[11] = Convert.ToInt32(_VerifyCircuitId.Rows[0]["VerifyCircuitId"].ToString());
                    _value[12] = objUserInfo.UserName;
                    if (this.gdvQuarterPlan.Rows[i].Cells[10].Text == "&nbsp;" || this.gdvQuarterPlan.Rows[i].Cells[10].Text == "&" || this.gdvQuarterPlan.Rows[i].Cells[10].Text == "<br>" || this.gdvQuarterPlan.Rows[i].Cells[10].Text == "")
                    {
                        Utility.HttpHelp.alert("评估人不可为空！", Page);
                        return;
                    }
                    else
                    {
                        _value[13] = this.gdvQuarterPlan.Rows[i].Cells[10].Text.Trim();
                    }
                    _value[14] =  this.gdvQuarterPlan.Rows[i].Cells[1].Text.Trim() ;
                    _value[15] = "新增";
                    _valueList.Add(_value);

                }
                else
                {
                    _count++;
                    if (Convert.ToInt32(this.gdvQuarterPlan.DataKeys[i]["PlanSendState"].ToString()) == 0 || Convert.ToInt32(this.gdvQuarterPlan.DataKeys[i]["PlanSendState"].ToString()) == -1)
                    {
                        if (this.gdvQuarterPlan.Rows[i].Cells[8].Text == "&nbsp;" || this.gdvQuarterPlan.Rows[i].Cells[8].Text == "&" || this.gdvQuarterPlan.Rows[i].Cells[8].Text == "<br>")
                        {
                            Utility.HttpHelp.alert("配合部门不可为空！", Page);
                            return;
                        }
                        if (this.gdvQuarterPlan.Rows[i].Cells[8].Text.Contains("独立完成"))
                        {
                        }
                        else
                        {
                            if (this.gdvQuarterPlan.Rows[i].Cells[9].Text == "&nbsp;" || this.gdvQuarterPlan.Rows[i].Cells[9].Text == "&" || this.gdvQuarterPlan.Rows[i].Cells[9].Text == "<br>")
                            {
                                Utility.HttpHelp.alert("配合部门责任不可为空！", Page);
                                return;
                            }
                        }
                        if (this.gdvQuarterPlan.Rows[i].Cells[10].Text == "&nbsp;" || this.gdvQuarterPlan.Rows[i].Cells[10].Text == "&" || this.gdvQuarterPlan.Rows[i].Cells[10].Text == "<br>")
                        {
                            Utility.HttpHelp.alert("评估人不可为空！", Page);
                            return;
                        }
                        string a = this.gdvQuarterPlan.DataKeys[i]["PlanSendState"].ToString();
                        if (Convert.ToInt32(this.gdvQuarterPlan.DataKeys[i]["PlanSendState"].ToString()) == 0 || Convert.ToInt32(this.gdvQuarterPlan.DataKeys[i]["PlanSendState"].ToString()) == -1)
                        {
                            editQuarterPlan(Convert.ToInt32(this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString()));
                        }
                    }
                }
            }
        }
        #endregion
        if (_count <= 0)
        {
            Utility.HttpHelp.alert("请选择要提交的计划", Page);
            return;
        }
        if (_valueList.Count > 0)
        {
            int result = dllQuarterPlanofManager.addWithTransaction(_valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("提交年计划未成功，请重试！");
            }
            else
            {
                Utility.HttpHelp.alert("提交年计划成功!", this.Page);
            }
        }
        bindGridViewQuarterPlan();
        #region 善后操作
        //将文本框清空
        this.txtQuarterPlanSortId.Value = dllQuarterPlanofManager.getPlanSort(this.drpQuarterDate.SelectedValue) + "." + Convert.ToString(this.gdvQuarterPlan.Rows.Count + 1);
        this.txtCompleteTime.Text = "";
        //this.txtMasterPeople.Value = "";
        this.txtRemark.Text = "";
        this.txtSlaveDepartment.Value = "";
        this.txtSlaveDepartmentDuty.Text = "";
        this.txtStartTime.Text = "";
        this.txtVerifyPeople.Value = "";
        this.txtWorkItem.Text = "";
        #endregion
    }

    /// <summary>
    /// 修改年计划方法
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-5</date>
    void editQuarterPlan(int _quarterPlanId)
    {
        object[] _value = new object[1];
        _value[0] = _quarterPlanId;
        int result = dllQuarterPlanofManager.edit(QuarterPlanofManager.field.PlanSendState.ToString(), _value);
        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("修改年计划未成功，请重试！");
        }
        else
        {
            Utility.HttpHelp.alert("提交年计划成功!", this.Page);
        }

        #region 善后操作
        //将文本框清空
        //this.txtQuarterPlanSortId.Value = "";
        //this.txtCompleteTime.Text = "";
        //this.txtMasterPeople.Value = "";
        //this.txtRemark.Text = "";
        //this.txtSlaveDepartment.Value = "";
        //this.txtSlaveDepartmentDuty.Text = "";
        //this.txtStartTime.Text = "";
        //this.txtVerifyPeople.Value = "";
        //this.txtWorkItem.Text = "";
        #endregion
    }


    /// <summary>
    /// 删除按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-17</date>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引



        //遍历GridView将GridView转为DataTable
        DataTable _quarterPlan = createDataTable();
        //获取GridView当前内容导入DataTable
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            DataRow _drGridView = _quarterPlan.NewRow();

            _drGridView["QuarterPlanSortId"] = this.gdvQuarterPlan.Rows[i].Cells[1].Text;
            _drGridView["StartTime"] = this.gdvQuarterPlan.Rows[i].Cells[2].Text.ToString();
            _drGridView["CompleteTime"] = this.gdvQuarterPlan.Rows[i].Cells[3].Text.ToString();
            _drGridView["WorkItem"] = this.gdvQuarterPlan.Rows[i].Cells[4].Text.ToString();
            _drGridView["MasterDepartment"] = this.gdvQuarterPlan.Rows[i].Cells[5].Text.ToString();
            _drGridView["MasterPeople"] = this.gdvQuarterPlan.Rows[i].Cells[6].Text.ToString();
            _drGridView["SlaveDepartment"] = this.gdvQuarterPlan.Rows[i].Cells[7].Text.ToString();
            _drGridView["SlaveDepartmentDuty"] = this.gdvQuarterPlan.Rows[i].Cells[8].Text.ToString();
            _drGridView["EvaluatePeople"] = this.gdvQuarterPlan.Rows[i].Cells[9].Text.ToString();
            _drGridView["Remart"] = this.gdvQuarterPlan.Rows[i].Cells[10].Text.ToString();
            _drGridView["QuarterPlanId"] = this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString();
            _drGridView["PlanSendState"] = this.gdvQuarterPlan.DataKeys[i]["PlanSendState"].ToString();

            #region 去除特殊符号如&nbsp;
            _drGridView["PlanSendState"] = _drGridView["PlanSendState"].ToString().Replace("&", "");
            _drGridView["PlanSendState"] = _drGridView["PlanSendState"].ToString().Replace("nbsp;", "");
            _drGridView["QuarterPlanId"] = _drGridView["QuarterPlanId"].ToString().Replace("&", "");
            _drGridView["QuarterPlanId"] = _drGridView["QuarterPlanId"].ToString().Replace("nbsp;", "");
            _drGridView["QuarterPlanSortId"] = _drGridView["QuarterPlanSortId"].ToString().Replace("&", "");
            _drGridView["QuarterPlanSortId"] = _drGridView["QuarterPlanSortId"].ToString().Replace("nbsp;", "");
            _drGridView["StartTime"] = _drGridView["StartTime"].ToString().Replace("&", "");
            _drGridView["StartTime"] = _drGridView["StartTime"].ToString().Replace("nbsp;", "");
            _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("&", "");
            _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("nbsp;", "");
            _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("&", "");
            _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("nbsp;", "");

            _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("&", "");
            _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("nbsp;", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("&", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("nbsp;", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace(",<br>", ",");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("&", "");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("nbsp;", "");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace(",<br>", ",");

            _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("&", "");
            _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("nbsp;", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("&", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("nbsp;", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace(",<br>", ",");

            _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("&", "");
            _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("nbsp;", "");
            #endregion

            _quarterPlan.Rows.Add(_drGridView);
        }

        //计划如果被保存为草稿，也同时删除
        if (this.gdvQuarterPlan.DataKeys[_rowIndex]["QuarterPlanId"].ToString() != "&nbsp;" && this.gdvQuarterPlan.DataKeys[_rowIndex]["QuarterPlanId"].ToString().Length > 0)
        {
            if (Convert.ToInt32(this.gdvQuarterPlan.DataKeys[_rowIndex]["PlanSendState"].ToString()) != 0)
            {
                Utility.HttpHelp.alert("已提交的计划不可以删除", Page);
                return;
            }
            _quarterPlan.Rows[_rowIndex].Delete();
            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(this.gdvQuarterPlan.DataKeys[_rowIndex]["QuarterPlanId"].ToString());
            int result = dllQuarterPlanofManager.del(QuarterPlanofManager.field.QuarterPlanId.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("删除年计划未成功，请重试！");
            }
        }
        else
        {
            _quarterPlan.Rows[_rowIndex].Delete();
        }
        this.gdvQuarterPlan.DataSource = _quarterPlan;
        this.gdvQuarterPlan.DataBind();
        //隐藏首行，只保留表头

        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            wrapByComma(this.gdvQuarterPlan, i, 6);
            wrapByComma(this.gdvQuarterPlan, i, 7);
        }
        this.txtQuarterPlanSortId.Value = dllQuarterPlanofManager.getPlanSort(this.drpQuarterDate.SelectedValue) + "." + Convert.ToString(this.gdvQuarterPlan.Rows.Count + 1);
    }

    /// <summary>
    /// 分解所选年计划，弹出添加分解窗体
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-17</date>
    protected void btnQuarterResolve_Click(object sender, EventArgs e)
    {
        //计数器
        int _count = 0;
        //年计划id 
        string _quarterPlanId = "";
        string _quarterPlanSortId = "";
        //年计划审核流程id
        int _verifyCircuitId = 0;
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        //所在部门
        string _masterDepartment = this.drpDepartment.SelectedItem.Text;
        DataTable _verifyCircuitTable = dllVerifyCircuitManager.getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_verifyCircuitTable.Rows.Count == 0)
        {
            Utility.Error.RedirectErrorPage("所在部门无计划流程，请重试");
        }
        else
        {
            _verifyCircuitId = Convert.ToInt32(_verifyCircuitTable.Rows[0]["VerifyCircuitId"].ToString());
        }

        //年计划所在周期
        int _quarterDate = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
        //发起人
        Model.UserInfo objUserInfo = new Model.UserInfo();
        string _sponsor = objUserInfo.UserName;
        //获得所选年计划的id
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            CheckBox _chk = (CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (_chk.Checked == true)
            {
                if (this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString().Length > 0 && this.gdvQuarterPlan.DataKeys[i]["PlanSendState"].ToString() == "0")
                {
                    _quarterPlanId = this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString();
                    _quarterPlanSortId = this.gdvQuarterPlan.Rows[i].Cells[1].Text;
                }
                _count++;
            }
        }
        if (_quarterPlanId.Length == 0)
        {
            Utility.HttpHelp.alert("计划为草稿状态才可分解", Page);
            return;
        }
        if (_count != 1)
        {
            Utility.HttpHelp.alert("只能选择一个年计划", Page);
            return;
        }
        else
        {
            //定向到添加年分解页面
            string _url = "QuarterResolveAdd.aspx?_quarterPlanId=" + _quarterPlanId + "&_quarterPlanSortId=" + _quarterPlanSortId;
            Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=200,width=902,top=300,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no');</script>");

        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个年计划", Page);

        }
    }



    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-19</date>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = false;
            }
        }

    }


    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvQuarterPlan_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvQuarterPlan.PageIndex = e.NewPageIndex;
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
        DataTable _quarterPlan = dllQuarterPlanofManager.get(QuarterPlanofManager.field.QuarterDate.ToString(), _value);
        DataRow dr = _quarterPlan.NewRow();
        _quarterPlan.Rows.InsertAt(dr, 0);
        this.gdvQuarterPlan.DataSource = _quarterPlan;
        this.gdvQuarterPlan.DataBind();
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {

            wrapByComma(this.gdvQuarterPlan, i, 6);
            wrapByComma(this.gdvQuarterPlan, i, 7);
        }

    }
    /// <summary>
    /// 年份索引改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
        bindGridViewQuarterPlan();
    }

    /// <summary>
    /// 年周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridViewQuarterPlan();
        this.txtQuarterPlanSortId.Value = dllQuarterPlanofManager.getPlanSort(this.drpQuarterDate.SelectedValue) + "." + Convert.ToString(this.gdvQuarterPlan.Rows.Count + 1);
    }


    /// <summary>
    /// 部门改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridViewQuarterPlan();
        bindDropDownList(this.drpMasterPeople);
        this.txtMasterDepartment.Text = this.drpDepartment.SelectedItem.Text;
        BindDepartmentInfo();
    }


    /// <summary>
    /// 来源时间
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtnSource_Click(object sender, EventArgs e)
    {
        int i = 0;
        LinkButton bt = (LinkButton)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        Control c = (Control)iRow.Parent;
        GridView _gdv = (GridView)c.Parent;

        int TotalPlanId = Convert.ToInt32(_gdv.DataKeys[i]["TotalPlanId"].ToString());
        if (TotalPlanId == 0)
        {
            //新增的月计划，不需要跳转到指定页面
        }
        else
        {
            string _url = "TotalPlanDetail.aspx?TotalPlanId=" + TotalPlanId;
            Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','height=200,width=900,top=200,left=50,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
        }
    }


    /// <summary>
    /// 修改计划
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModify_Click(object sender, EventArgs e)
    {
        int _count = 0;
        int _quarterPlanId = 0;
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                _count++;
                _quarterPlanId = Convert.ToInt32(this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString());
            }
        }
        if (_count == 1)
        {
            //判断是否为评估人，如果该条计划的评估人是当前用户则不能修改该条计划
            object[] _value = new object[1];
            _value[0] = _quarterPlanId;
            DataTable quarterplan = dllQuarterPlanofManager.get(QuarterPlanofManager.field.QuarterPlanId.ToString(), _value);
            if (quarterplan.Rows.Count > 0)
            {
                if (Convert.ToInt32(quarterplan.Rows[0]["PlanSendState"]) >= 1)
                {
                    Utility.HttpHelp.alert("计划已提交，不允许修改！", Page);
                    return;
                }
            }
            Model.UserInfo objUser = new Model.UserInfo();
            object[] _user = new object[1];
            _user[0] = objUser.UserName;
            DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _user);
            if (_userTable.Rows.Count > 0)
            {
                object[] _username = new object[2];
                _username[0] = _userTable.Rows[0]["RealName"].ToString();
                _username[1] = _quarterPlanId;
                DataTable _iSEvaluatePeople = dllQuarterPlanofManager.get(QuarterPlanofManager.field.EvaluatePeople.ToString(), QuarterPlanofManager.field.QuarterPlanId.ToString(), _username);
                if (_iSEvaluatePeople.Rows.Count > 0)
                {
                    Utility.HttpHelp.alert("评估人不能修改计划", Page);
                }
                else
                {
                    ListItem _li = this.drpQuarterDate.SelectedItem;
                    if (_li != null)
                    {
                        string _url = "ModifyQuarterPlanDraft.aspx?_quarterPlanId=" + _quarterPlanId + "&_quarterDate=" + _li.Value + "&url=QuarterPlanAdd.aspx";
                        Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=200,width=900,top=300,left=20,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
                        return;
                    }
                }
            }

        }
        else
        {
            Utility.HttpHelp.alert("请选择一个计划", Page);
        }
    }

    /// <summary>
    /// 下载按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvQuarterPlan.DataKeys[i]["AttachmentPath"].ToString();
        // Response.Redirect("~/upload/"+path);
        FileDownload(path);
        // Utility.HttpHelp.DownloadFile(path);
    }

    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {

        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }
}
